﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 解释器模式
{
    class Program
    {
        static void Main(string[] args)
        {
            PlayContent context = new PlayContent();
            //音乐-上海滩
            Console.WriteLine("上海滩：");
            context.txt = "T 500 O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 ";

            Expression expression = null;
            try
            {
                while (context.txt.Length > 0)
                {
                    string str = context.txt.Substring(0, 1);
                    switch (str)
                    {
                        case "O":
                            expression = new Scale();
                            break;
                        case "T":
                            expression = new Speed();
                            break;
                        case "C":
                        case "D":
                        case "E":
                        case "F":
                        case "G":
                        case "A":
                        case "B":
                        case "P":
                            expression = new Note();
                            break;

                    }
                    //解释
                    expression.InterPreter(context);
                }

            }
            catch (Exception ex)
            {

                Console.WriteLine(ex);
            }

            Console.Read();


        }
    }


    /// <summary>
    /// 演奏内容
    /// </summary>
    public class PlayContent
    {
        public string txt { get; set; }
    }

    /// <summary>
    /// 表达式
    /// </summary>
    abstract class Expression
    {

        /// <summary>
        ///解释器
        /// </summary>
        /// <param name="content">要解释的内容</param>
        public void InterPreter(PlayContent context)
        {
            if (context.txt.Length == 0)
            {
                return;
            }
            else
            {
                string playKey = context.txt.Substring(0, 1);
                context.txt = context.txt.Substring(2);
                double playValue = Convert.ToDouble(context.txt.Substring(0, context.txt.IndexOf(" ")));
                context.txt = context.txt.Substring(context.txt.IndexOf(" ") + 1);

                Excute(playKey, playValue);
            }
        }
        //执行
        public abstract void Excute(string key, double value);

    }


    //音符
    class Note : Expression
    {
        public override void Excute(string key, double value)
        {
            string note = "";
            switch (key)
            {
                case "C":
                    note = "1";
                    break;
                case "D":
                    note = "2";
                    break;
                case "E":
                    note = "3";
                    break;
                case "F":
                    note = "4";
                    break;
                case "G":
                    note = "5";
                    break;
                case "A":
                    note = "6";
                    break;
                case "B":
                    note = "7";
                    break;

            }
            Console.Write("{0} ", note);

        }
    }


    //音阶
    class Scale : Expression
    {
        public override void Excute(string key, double value)
        {
            string scale = "";
            switch (Convert.ToInt32(value))
            {
                case 1:
                    scale = "低音";
                    break;
                case 2:
                    scale = "中音";
                    break;
                case 3:
                    scale = "高音";
                    break;

            }
            Console.Write("{0} ", scale);
        }
    }
    //音速
    class Speed : Expression
    {
        public override void Excute(string key, double value)
        {
            string speed;
            if (value < 500)
                speed = "快速";
            else if (value >= 1000)
                speed = "慢速";
            else
                speed = "中速";

            Console.Write("{0} ", speed);
        }
    }



}
